<template>
  <div class="check2" style="background-color: #ffffff">

    <div class="search1">
        <div class="find">
            <form action="###" class="searchForm">
          <input
            type="text"
            style="vertical-align: top;"
            placeholder="输入关键词"
            v-model="keywords"
          >
          <button
            class="btn"
            type="button"
            @click="goSearch"
            style="font-size:16px; font-weight:blod"
          >
            搜索
          </button>
           <el-button @click="delay" style="margin-left:10px;vertical-align: top;">取消</el-button>
             <el-select @change="selectButton" v-model="selected"  style="margin-left:20px" >
             <el-option value="" label="请选择" />
             <el-option

                v-for="item in goodsstatus"
                :key="item"     
                :label="item"
                :value="item"
  
                
             />
    </el-select>
          <el-button @click="toggleSelection()" style="font-weight:bold;vertical-align: top;">取消选择</el-button>
            <el-button @click="allRemove" style="font-weight:bold;vertical-align: top;">删除</el-button>
            
          </form>
      </div>
    <el-table
    class="table2"
     ref="multipleTable"
    :data="tableData"
    tooltip-effect="dark"
    @selection-change="handleSelectionChange"
      style="width: 100%"
      max-height="500"
    >
     <el-table-column
      type="selection"
      width="35">
      </el-table-column>
      <el-table-column fixed prop="id" label="货物Id" width="100">
      </el-table-column>

      <el-table-column prop="name" label="货物名称" width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.id==inputHandle.id">
            <span v-if="modifyFlag">{{scope.row.name}}</span>
            <el-input v-model="scope.row.name" v-else value="text"></el-input>
          </div>
          <span v-else>{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="number" label="货物数量" width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.id==inputHandle.id">
            <span v-if="modifyFlag">{{scope.row.number}}</span>
            <el-input v-model="scope.row.number" v-else value="number"></el-input>
          </div>
          <span v-else>{{scope.row.number}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="locationId" label="库区" width="100">
      </el-table-column>
      <el-table-column prop="warehouseEntryTime" label="入库时间" width="120">
      </el-table-column>
      <el-table-column prop="deadlineTime" label="存放截止时间" width="160">
        <!-- 当选择的id和表格的当前行的id匹配则显示input -->
        <template slot-scope="scope">
          <div v-if="scope.row.deadlineTime==inputHandle.deadlineTime&&scope.row.id==inputHandle.id">
            <span  v-if="modifyFlag">{{scope.row.deadlineTime}}</span>
               <el-date-picker
                 v-model="scope.row.deadlineTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="scope.row.deadlineTime"  v-else>
    </el-date-picker>

          </div>
          <span v-else>{{scope.row.deadlineTime}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="货物状态" width="120">
        <template slot-scope="scope">
          <div v-if="scope.row.id==inputHandle.id">
            <span v-if="modifyFlag">{{scope.row.status}}</span>
            <el-input v-model="scope.row.status" v-else></el-input>
          </div>
          <span v-else>{{scope.row.status}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <template v-if="scope.row.id==inputHandle.id">
              <el-button
             @click="ModifyRow(scope.row)"
              v-if="modifyFlag"
              type="text"
              size="small"
              style="font-weight:bold"
        >
          修改
      </el-button>
      <el-button
          @click="save(scope.row)"
          v-else
          type="text"
          size="small"
      >
          保存
      </el-button>
        </template>

        <template v-else>
           <el-button
             @click="ModifyRow(scope.row)"
              type="text"
              size="small">
             修改
          </el-button>
        </template>
        <el-button
          @click.native.prevent="deleteRow(scope.row)"
          type="text"
          size="small"
      >
         删除
      </el-button>
      </template>
      </el-table-column>

    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.page.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="this.page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.page.total"
      >
      </el-pagination>
    </div>
    </div>
  </div>
</template>

<script>
import axios from '../../../axios'
export default {
  name: 'remove',
  inject: ['reload'],
  data () {
    return {
      tableData: [{}],
      selected:"",
      goodsstatus:[
        "入库待核验",
         "已入库",
         "出库待核验",
         "已出库"
      ],
      keywords: '',
      searchBoolean: false,
      modifyFlag: true,
      inputHandle: { },
      delete: {
        id: '',
        pageSize: '',
        ids: []
      },
      page: {
        // 当前页码
        currentPage: 1,
        // 每页条数
        pageSize: 10,
        total: '',
        checked: true

      }
    }
  },
  methods: {
     selectButton(){
   axios({
        url: '/dp/goods/QueryByBlurry',
        method: 'post',
        data:{
                  blurryString: this.selected,
                  pageSize: this.page.pageSize,
                  currentPage: this.page.currentPage
                }
               ,

      }).then(res => {
        // 拿到后端分页数据
        this.tableData = res.data.data.records
        this.searchBoolean = true
        // 拿到后端数据总条数
        this.page.total = parseInt(res.data.data.total)
      })
    },
    delay () {
      this.keywords = '',

      axios({
        url: '/dp/goods/QueryByBlurry',
        method: 'post',
        Data:
                {
                  blurryString: this.keywords,
                  pageSize: this.page.pageSize,
                  currentPage: this.page.currentPage
                },
      }).then(res => {
        // 拿到后端分页数据
        this.tableData = res.data.data.records
        this.searchBoolean = true
        // 拿到后端数据总条数
        this.page.total = parseInt(res.data.data.total)
      })
    },
    // 模糊查询分页调用共同函数
    // 模糊查询分页调用共同函数
    serarch () {
      axios({
        url: '/dp/goods/QueryByBlurry',
        method: 'Post',
        data:
                {
                  blurryString: this.keywords,
                  pageSize: this.page.pageSize,
                  currentPage: this.page.currentPage
                },
      }).then(res => {
        // 拿到后端分页数据
        this.tableData = res.data.data.records
        this.searchBoolean = true
        // 拿到后端数据总条数
        this.page.total = parseInt(res.data.data.total)
      })
    },
    // 模糊查询
    goSearch () {
      this.page.currentPage = 1,
      this.serarch()
    },
    // 条数选择
    handleSizeChange (val) {
      this.page.pageSize = val
      this.page.currentPage = 1
      if (this.searchBoolean) {
        // 如果执行了模糊查询则调用该接口分页
        this.serarch()
        return
      }
      axios({
        url: '/dp/goods/query',
        method: 'post',
        // 上传page参数,让后端进行分页
        data: JSON.stringify(this.page)
      }).then(res => {
        // 拿到后端分页数据
        this.tableData = res.data.data.records
        // 拿到后端数据总条数
        this.page.total = parseInt(res.data.data.total)
      })
    },
    // 下一页
    handleCurrentChange (val) {
      this.page.currentPage = val
      console.log(`当前页: ${val}`)
      if (this.searchBoolean) {
        // 如果执行了模糊查询则调用该接口分页
        this.serarch()
        return
      }
      axios({
        url: '/dp/goods/query',
        method: 'post',
        // 需要想办法传入page对象
        data: JSON.stringify(this.page)
      }).then(res => {
        this.tableData = res.data.data.records
      }).catch(err => {
        this.$alert(err.response.data.msg, '请联系管理员', {
          confirmButtonText: '确定',
          callback: action => {

          }
        })
      })
    },
    save (row) {
      // 保存的修改
      console.log(row)
      axios({
        url: '/dp/goods/update',
        method: 'POST',
        data: JSON.stringify(row)
      }).then(res => {
        this.tableData = res.data.data.records
        this.page.total = parseInt(res.data.data.total)
      }).catch(err => {
        this.$alert(err.response.data.msg, '请联系管理员', {
          confirmButtonText: '确定',
          callback: action => {

          }
        })
      })
      this.modifyFlag = !this.modifyFlag,
      this.reload()
    },
    ModifyRow (row) {
      this.modifyFlag = !this.modifyFlag,
      this.inputHandle = row
    },
    // 批量删除
    allRemove () {
      this.delete.pageSize = this.page.pageSize
      axios({
        url: '/dp/goodsList',
        method: 'delete',
        data: {
          data: this.delete
        }
      }).then(res => {
        this.tableData = res.data.data.records
        this.page.total = parseInt(res.data.data.total)
      }).catch(err => {
        this.$alert(err.response.data.msg, '请联系管理员', {
          confirmButtonText: '确定',
          callback: action => {

          }
        })
      })
      this.reload()
    },
    // 单行删除
    deleteRow (val) {
      // 删除的id
      this.delete.id = val.id
      // 刷新的页面条数,page定义要在del后
      this.delete.pageSize = this.page.pageSize
      console.log(this.delete)
      axios({
        url: '/dp/goods',
        method: 'delete',
        data: {
          data: this.delete
        }
      }).then(res => {
        this.tableData = res.data.data.records
        this.page.total = parseInt(res.data.data.total)
      }).catch(err => {
        this.$alert(err.response.data.msg, '请联系管理员', {
          confirmButtonText: '确定',
          callback: action => {

          }
        })
      })
      this.reload()
    },
    toggleSelection (rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    // 选中的数据
    handleSelectionChange (val) {
      var array = []
      this.multipleSelection = val
      val.forEach(element => {
        array.push(element.id)
      })
      this.delete.ids = array
    }
  },

  created () {
    // 页面刷新时重置搜索框和按键状态
    this.keywords = '',
    this.searchBoolean = false,
    console.log(this.page)
    axios({
      url: '/dp/goods/query',
      method: 'post',
      // 需要想办法传入page对象
      data: JSON.stringify(this.page)
    }).then(res => {
      this.tableData = res.data.data.records
      this.page.total = parseInt(res.data.data.total)
      console.log(this.page.total)
    }).catch(err => {
      console.log(err.response)
      this.$alert(err.response.data.msg, '请联系管理员', {
        confirmButtonText: '确定',
        callback: action => {

        }
      })
    })
  }
}
</script>
<style lang="less" >
  .check2 {

    width: 1200px;
    margin: 30px auto;
    display: flex;
    position: relative;
    height: 630px;

    .cell
{
  font-weight: bold;
  font-size: 20px;
  color:rgb(61, 59, 59);
}
.el-pagination{
  .el-pagination__total{
  font-weight: bold;
  font-size: 20px;
  color:rgb(61, 59, 59);
}
.el-input__inner{
  font-weight: bold;
  font-size: 20px;
  color: rgb(61, 59, 59);
}
.button{
  font-weight: bold;
  font-size: 20px;
  color: rgb(61, 59, 59);
}
.el-pager
.number{
  font-weight: bold;
  font-size: 20px;

}
.el-pagination__jump{
  font-weight: bold;
  font-size: 20px;
  color: rgb(61, 59, 59);
}
}
   .search1 {
      float: left;
      margin-left:50px;
      margin-top: 30px;
      .searchForm {
        overflow: hidden;
        margin-right: 0px;

        input {
          box-sizing: border-box;
          width: 300px;
          height: 38px;
          padding: 0px 4px;
          border: 2px solid rgb(8, 138, 8);
          float: left;

          &:focus {
            outline: none;
          }
        }

        .btn {
          height: 38px;
          width: 68px;
          background-color: #0f778a;
          border: none;
          color: #fff;
          float: left;
          cursor: pointer;

          &:focus {
            outline: none;
          }
        }}}
 .table2{
     width: 980px!important;
   }

}
</style>
